// pages/01homePage/06order/manage.js
var s = require('../../js/sha1.js')
var app = getApp()
var appData = app.globalData
Page({
   data: {
      order: '',
      table: '',
      printerList: [],
      showPrint: 0,
      _type: 0 //0打印全部，1打印加菜
   },

   onLoad: function (opt) {
      console.log('【全局】appData---------', appData)
      console.log('【本页】this.data---------', this.data)
      var obj = JSON.parse(opt.table)
      wx.setNavigationBarTitle({
         title: obj.fenlei + obj.zhuo + '菜单',
      })
      var param = {
         seat_fee: appData.shopInfo.shopInfo.seat_fee,
         orderid: obj.orderid
      }
      var that = this
      app.getJsonData('api/space/table_eat_bill.php', param, function (res) {
         that.setData({
            order: res.data,
            table: obj
         })
         console.log('成功获取账单that.data', that.data)
      })
   },

   listPrinter: function (e) {
      var t = this
      t.setData({ _type: e.currentTarget.id })//0打印全部，1打印加菜
      if (t.data.printerList.length == 0) {
         var param = { shop_id: appData.shopInfo.shopInfo.userid }
         app.getNetData('print2/mgr/getprinterlist', param, function (res) {
            t.setData({ printerList: res.data })
            t.checkPrint()
         })
      } else {
         t.checkPrint()
      }
   },

   checkPrint: function () {//检查是否可以直接打印
      if (this.data.printerList.length == 1) {
         var e = { currentTarget: { id: 0 } }
         this.clickSendPrint(e);
      } else {
         this.setData({ showPrint: 1 })
      }
   },

   clickSendPrint: function (e) {//多台打印机时，由用户选择打印
      var i = e.currentTarget.id
      var t = this
      var td = t.data
      var printer = td.printerList[i]
      var param = {
         order_id: td.table.orderid,
         seat_fee: appData.shopInfo.shopInfo.seat_fee,
         shop_name: appData.shopInfo.shopInfo.shop,
         table_name: td.table.fenlei + td.table.zhuo,
         type: td._type//0打印全部，1打印加菜
      }
      if (printer.brand_en == 'pc_printer') {
         param.needImg = 0
         t.sendNetPrint(param)
      } else {
         param.printer_brand = printer.brand
         param.printer = printer.setup_params
         app.getNetData('print2/mgr/printTicket', param, function (res) {
            if (td.showPrint) {
               t.setData({ showPrint: 0 })
            }
         })
      }
   },
   //发送到传统共享打印机
   sendNetPrint: function (params) {
      var td = this.data
      var json = {
         type: 'say',
         to_client_id: 'all',
         param: params
      }
      console.log('发送打印数据json==', json)
      var msg = JSON.stringify(json)
      appData.isWsSender = 1
      app.sendSocketMsg(msg)
      if (td.showPrint) {
         t.setData({ showPrint: 0 })
      }
   },

   maskHide: function () {
      this.setData({ showPrint: 0 })
   },
})

